package com.hsogoo.study.concurrency;

/**
 * Created by za-huangsaigang on 2018/8/29.
 */
public class WaitTest {

    public static void main(String[] args) {

        WaitThread wt = new WaitThread("wt");

        synchronized (wt){
            System.out.println(Thread.currentThread().getName()+" start wt");
            wt.start();

            System.out.println(Thread.currentThread().getName()+" block");
            try {
                wt.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+" continue");
        }

    }

    static class WaitThread extends Thread{

        public WaitThread(String name) {
            super(name);
        }

        public void run() {
            synchronized (this) { // 通过synchronized(this)获取“当前对象的同步锁”
                System.out.println(Thread.currentThread().getName()+" wakeup others");
                notify();    // 唤醒“当前对象上的等待线程”
            }
        }
    }
}
